package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.pojo.Category;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Override
    public Result findAll() {
        List<Category> categoryList = this.list();
        if (categoryList.size() == 0) {
            return new Result(false, StatusCode.ERROR, "没有查询到分类");
        }
        return new Result(true, StatusCode.OK, null, categoryList);
    }


    @Override
    public Result add(Category category) {
        boolean save = this.save(category);
        if (!save) {
            return new Result(false, StatusCode.ERROR, "存入数据失败");
        }
        return new Result(true, StatusCode.OK, null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        Page<Category> categoryPage = new Page<>(page, size);
        this.page(categoryPage);
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setRows(categoryPage.getRecords());
        categoryPageResult.setTotal(categoryPage.getTotal());
        return new Result(true, StatusCode.OK, null, categoryPageResult);
    }

    @Override
    public Result findPage(Integer page, Integer size, Category category) {
        Page<Category> categoryPage = new Page<>(page, size);
        LambdaQueryWrapper<Category> lambdaQuery = Wrappers.lambdaQuery(Category.class);
        if (category.getName() != null) {
            lambdaQuery.like(Category::getName, category.getName());
        }
        if (category.getIsShow() != null) {
            lambdaQuery.eq(Category::getIsShow, category.getIsShow());
        }
        if (category.getIsMenu()!=null){
            lambdaQuery.eq(Category::getIsMenu,category.getIsMenu());
        }
        if (category.getParentId()!=null){
            lambdaQuery.eq(Category::getParentId,category.getParentId());
        }
        if (category.getSeq()!=null){
            lambdaQuery.eq(Category::getSeq,category.getSeq());
        }
         this.page(categoryPage, lambdaQuery);
        List<Category> records = categoryPage.getRecords();
        if (records.size()==0){
            return new Result(false, StatusCode.ERROR, "没有查询到数据");
        }
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setTotal(categoryPage.getTotal());
        categoryPageResult.setRows(categoryPage.getRecords());
        return new Result(true, StatusCode.OK, null,categoryPageResult );
    }

    @Override
    public Result findById(Integer id) {
        Category byId = this.getById(id);
        if (byId == null) {
            return new Result(false, StatusCode.ERROR, "没有查询到数据");
        }
        return new Result(true, StatusCode.OK, null, byId);
    }

    @Override
    public Result updateById(Integer id, Category category) {
        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery(Category.class);
        queryWrapper.eq(Category::getId,id);
        boolean b = this.update(category, queryWrapper);
        if (!b) {
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @Override
    public Result deleteById(Integer id) {
        boolean b = this.removeById(id);

        if (!b) {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
        return new Result(true, StatusCode.OK, "删除成功");
    }


}
